<?php

namespace App\Models\Shop;

use App\Models\BaseModel;

class ShopCategoryModel extends BaseModel
{
    protected $table = 'shop_category';

    protected $primaryKey = 'id';

    public $dateFormat = 'U';

    public function getTrees($default = true)
    {
        $list = self::where('status',1)->select('id','parent_id','name')->get()->toArray();
        $category = [];
        if($default){
            $category =  [['value'=>'0','label'=>'顶级分类']];
        }
        if($list){
            $category = array_merge($category,format_cascade($list,'id','name'));
        }
        return $category;
    }

    public function getDTree($is_show = false)
    {
        $list = self::whereIn('status',[0,1])->select('id','name','name_mobile','parent_id','parent_ids','status')->get()->toArray();
        if($list){
            $new_list = [];
            foreach ($list as $key=>$item){
                $status = $item['status']?'启用':'禁用';
                $new_list[$key] = $item;
                $new_list[$key]['parentId'] = $item['parent_id'];
                $new_list[$key]['title'] = $item['name'].'['.$item['name_mobile'].']'.'['.$status.']';
                $new_list[$key]['isLast'] = in_array($item['id'],array_column($list,'parent_id'))?false:true;
                $new_list[$key]['level'] = count(str2arr($item['parent_ids']))+1;
                $new_list[$key]['checkArr'] = [['type'=>0,'isChecked'=>0]];
                if($is_show){
                    $new_list[$key]['basicData'] = [
                        'nodeName'=>$item['name'],
                        'editUrl'=>route('b_ShopCategory_update',['id'=>$item['id']]),
                        'deleteUrl'=>route('b_ShopCategory_delete',['id'=>$item['id']]),
                        'switchUrl'=>route('b_ShopCategory_switchCategory',['id'=>$item['id']]),
                    ];
                }
                unset($new_list[$key]['parent_id'],$new_list[$key]['parent_ids']);
            }
            $list = list_to_tree($new_list,true,'id','parentId','children');
        }

        return $list;
    }

    public function add($data)
    {
        $category = new self();
        $parent_id = 0;
        $parent_ids = '';
        if(isset($data['parents'])){
            $parents = $this->handleParents($data['parents']);
            $parent_id = $parents['parent_id'];
            $parent_ids = $parents['parent_ids'];
        }
        $category->name = $data['name'];
        $category->name_mobile = $data['name_mobile'];
        $category->description = $data['description'];
        $category->parent_id = $parent_id;
        $category->parent_ids = $parent_ids;
        $category->level = count(str2arr($parent_ids));
        $category->image = $data['image']??'';
        $category->sort = $data['sort']??1000;
        $category->is_hot = $data['is_hot']??0;
        $category->status = $data['status']??1;
        if (!$category->save()){
            return false;
        }
        return true;
    }

    public function edit($id, $data)
    {
        $category = self::find($id);
        if(!$category){
            $this->errorMsg ='分类信息不存在';
            return false;
        }
        $is_update = false;
        if(isset($data['parents'])){
            $parents = $this->handleParents($data['parents']);
            $parent_id = $parents['parent_id'];
            $parent_ids = $parents['parent_ids'];
            if($parent_id == $id||in_array($id,str2arr($parent_ids))){
                $this->errorMsg = '父级分类不能为自己或自己的下级!';
                return false;
            }
            if($category->parent_id!=$parent_id){
                $category->parent_id = $parent_id;
                $category->parent_ids = $parent_ids;
                $category->level = count(str2arr($parent_ids));
                $is_update = true;
            }
        }
        if(isset($data['name'])&&$data['name']!=$category->name){
            $category->name = $data['name'];
            $is_update = true;
        }
        if(isset($data['name_mobile'])&&$data['name_mobile']!=$category->name_mobile){
            $category->name_mobile = $data['name_mobile'];
            $is_update = true;
        }

        if(isset($data['description'])&&$data['description']!=$category->description){
            $category->description = $data['description'];
            $is_update = true;
        }

        if(isset($data['image'])&&$data['image']!=$category->image){
            $category->image = $data['image'];
            $is_update = true;
        }

        if(isset($data['sort'])&&$data['sort']!=$category->sort){
            $category->sort = $data['sort'];
            $is_update = true;
        }

        if(isset($data['is_hot'])&&$data['is_hot']!=$category->is_hot){
            $category->is_hot = $data['is_hot'];
            $is_update = true;
        }
        if(isset($data['status'])&&$data['status']!=$category->status){
            $category->status = $data['status'];
            $is_update = true;
        }

        if (!$is_update||!$category->save()){
            return false;
        }
        return true;

    }

    private function handleParents($action_parents)
    {
        $parent_id = 0;
        $parent_ids = '';
        if($action_parents){
            $pids = array_filter(str2arr($action_parents));
            if(is_array($pids)&&!empty($pids)){
                $parent_id = end($pids);
                $parent_ids = arr2str($pids);
            }elseif(!is_array($pids)&&!empty($pids)){
                $parent_id = $pids;
                $parent_ids = $pids;
            }
        }

        return ['parent_id'=>$parent_id,'parent_ids'=>$parent_ids];
    }
}
